let express=require('express');
let multer = require('multer');
//配置multer
let storage = multer.diskStorage({
    destination: function (req, file, cb) {//配置目录
        cb(null, './public/uploads/')
    },
    filename: function (req, file, cb) {//配置文件名
        cb(null, decodeURI(file.originalname))
    }
})
let upload = multer({
    fileFilter: function (req, file, cb) {
        file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf-8')
        cb(null, true);
    },
    storage: storage
})

let router=express.Router();

router.post('/upload',upload.single('file'),function(req,res){
    console.log(req.file);
    if(req.file){
        res.send({code:200,path:'/uploads/'+req.file.originalname})
    }else{
        res.send({code:500,msg:'上传失败'})
    }
})
// router.post('/uploads',upload.array('files',3),function(req,res){
//     console.log(2,req.files);
//     if(req.files.length>0){
//         res.send({code:200,msg:'上传成功'})
//     }else{
//         res.send({code:500,msg:'上传失败'})
//     }
// })
router.get('/download',function(req,res){
    res.send({code:'200',url:'/uploads/a.txt'})
})

module.exports=router;